package com.access.boot.autoconfigure.apisdk.proxy.aop;

import com.access.boot.apisdk.SdkContext;
import com.access.boot.autoconfigure.apisdk.aop.MethodBeforeArgsChangeableAdvice;
import com.access.boot.autoconfigure.apisdk.context.SdkContextManager;
import com.access.boot.autoconfigure.apisdk.proxy.MethodWrapperCache;

import java.lang.reflect.Method;

/**
 * @author Yangzhen
 * @date 2022/6/4 13:50
 * @since 1.0.0
 */
public class SdkContextArgsBeforeChangeableAdvice implements MethodBeforeArgsChangeableAdvice {

    @Override
    public Object before(Method method, Object[] args, Object target) throws Throwable {

        // 判断执行的方法是否需要自动注入 SdkContext
        // 如果用户的接口声明的方法参数有 SdkContext，则说明用户要手动创建，否则底层自动创建
        boolean isAutoType = MethodWrapperCache.isAutoType(method);

        if (isAutoType) {
            // SdkContextManager 封装了域名、secret、token等服务的调用
            SdkContextManager instance = SdkContextManager.getInstance();
            SdkContext sdkContext = instance.getSdkContext();
            return sdkContext;
        }
        return null;
    }
}
